package bj.gui

import scala.swing.event
import event.ButtonClicked
import scala.swing.Frame
import scala.swing.Label
import scala.swing.Swing
import scala.swing.Button
import scala.swing.FlowPanel
import scala.swing.GridPanel
import scala.swing.BorderPanel
import scala.swing.Component
import javax.swing.SpinnerNumberModel
import javax.swing.JSpinner
import scala.swing.TextField
import javax.swing.ImageIcon
import bj.actor.House
import bj.actor.EdTable

class EditTable(val tableNum: String, val minBet: Double, val maxBet: Double, val dealerNm: String) extends Frame {
  javax.swing.UIManager.setLookAndFeel(Constants.LF)
  println("Edit Table window opened")
  title = "Blackjack - Edit Table"
  minimumSize = Constants.EditTableSize

  val dealerField = new TextField {
    columns = 8
    text = dealerNm
  }
  val minModel = new SpinnerNumberModel(minBet, 1, 5000, 1)
  val minSpinner = new JSpinner(minModel)

  val maxModel = new SpinnerNumberModel(maxBet, 1, 5000, 1)
  val maxSpinner = new JSpinner(maxModel)

  val okButton = new Button { text = "OK" }
  val cancelButton = new Button { text = "Cancel" }

  val tablePanel = new FlowPanel() {
    contents += new Label { text = "Table # " + tableNum }
    background = Constants.greenBack
  }
  val minPanel = new FlowPanel() {
    contents += new Label { text = "Min Bet: $" }
    contents += Component.wrap(minSpinner)
    background = Constants.greenBack
  }
  val maxPanel = new FlowPanel() {
    contents += new Label { text = "Max Bet: $" }
    contents += Component.wrap(maxSpinner)
    background = Constants.greenBack
  }
  val dealerPanel = new FlowPanel() {
    contents += new Label { text = "Dealer: " }
    contents += dealerField
    background = Constants.greenBack
  }
  val buttonPanel = new FlowPanel() {
    contents += okButton
    contents += cancelButton
    background = Constants.greenBack
  }
  val contentPanel = new GridPanel(5, 1) {
    contents += tablePanel
    contents += minPanel
    contents += maxPanel
    contents += dealerPanel
    contents += buttonPanel
    background = Constants.greenBack
  }
  contents = new BorderPanel() {
    add(new Label {
      icon = new ImageIcon("images/BJLogo2.gif")
    }, BorderPanel.Position.North)
    add(contentPanel, BorderPanel.Position.Center)
    border = Swing.EmptyBorder(20)
    background = Constants.greenBack
  }
  listenTo(okButton)
  listenTo(cancelButton)
  reactions += {
    case ButtonClicked(`okButton`) =>
      println("Clicked ok button")
      // Invoke method sendMsg in EyeInTheSky to send a message to the House actor to edit the table, providing min, max, and dealer name values from the GUI
      EyeInTheSky.sendMsg(EdTable(tableNum.toInt,minSpinner.getValue().toString().toDouble,maxSpinner.getValue().toString().toDouble,dealerField.text))
      EyeInTheSky.top.editTableButton.enabled = false                                          
      this.close()                                          
    case ButtonClicked(`cancelButton`) =>
      this.close()
  }
  visible = true
}